/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef DEVICE_MANAGER_H
#define DEVICE_MANAGER_H

#include <memory>
#include <string>
#include <sys/types.h>

#include "hmfs_data.h"
#include "hmfs_command.h"
#include "config.h"

namespace OHOS {
namespace Hmfs {

struct DeviceInfo {
    std::string path;
    int32_t fd = -1;
    uint32_t sectorSize = DEFAULT_SECTOR_SIZE;
    uint64_t sectorCount;
    uint64_t startBlkId;
    uint64_t endBlkId;
    uint32_t segmentCount;
    bool isMetaDevice = false;

    /* to handle zone block devices */
    int32_t zonedModel;
    uint32_t nrZones;
    uint32_t nrRndZones;
    size_t zoneBlocks;
    uint64_t zoneSize;
    std::unique_ptr<size_t[]> zoneCapBlocks = nullptr;
};

class DeviceManager{
public:
    ~DeviceManager() = default;
    DeviceManager(CmdConfig& cmdPara) : cmdPara_(cmdPara) {};

    static void CreateInstance(CmdConfig& cmdPara);
    static DeviceManager& GetInstance();
    int32_t CreateDeviceInfo(std::string& path, bool isMetaDevice);
    DeviceInfo* GetDeviceInfo(uint32_t deviceId);
    uint32_t GetDeviceCount();
    bool CheckDeviceFormated();
    int32_t TrimDevices();
    uint64_t GetTotalSectors();

private:
#ifdef HAVE_LIBBLKID
    bool IsFileSystemExist(DeviceInfo* deviceInfo);
#endif
    int32_t TrimDevice(DeviceInfo *deviceInfo);

    static std::unique_ptr<DeviceManager> instance_;
    std::vector<std::unique_ptr<DeviceInfo>> devices_;
    uint64_t totalSectors_ = 0;
    CmdConfig& cmdPara_;
};



} // namespace Hmfs
} // namespace OHOS
#endif
